package com.zham.springcloud;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@Autowired
	private CounterService counterService;

	private final Logger logger=Logger.getLogger(getClass());
	
	@Autowired
	private DiscoveryClient discoveryClient;
	
	@RequestMapping("/hello")
	public String index() {
		List<ServiceInstance> list = discoveryClient.getInstances("HELLO-SERVICE");
	    if (list != null && list.size() > 0 ) {
	        logger.info("/hello,host:"+list.get(0).getHost()+",service_id"+list.get(0).getServiceId());
	        
	    }
		return "Hello World";
	}

	@RequestMapping("/greet")
	public String greet() {
		counterService.increment("demo.hello.count");
		return "greet";
	}
}
